/**
 * 首页菜单
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home/Home' }">首页</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
</template>

<script>
import { getResponseList, addResponse, updateResponse, deleteResponse } from '../../api/botMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {},
      // rules表单验证
      rules: {
        responseType: [{ required: true, message: '请选择响应方式', trigger: 'blur' }],
        parsingMode: [{ required: true, message: '请选择解析方式', trigger: 'blur' }],
        content: [{ required: true, message: '请输入响应内容', trigger: 'blur' }],
        command: [{ required: true, message: '请输入指令', trigger: 'blur' }]
      },
      formInline: {
        pageNum: 1,
        pageSize: 10
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取响应列表
    getdata(parameter) {
      this.loading = true
      getResponseList(parameter)
      .then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          res.data.forEach(response => {
            response['command'] = response.commandList.map(item => item.command).join(',')
          })
          this.listData = res.data
          // 分页赋值
          this.pageparm.currentPage = this.formInline.pageNum
          this.pageparm.pageSize = this.formInline.pageSize
          this.pageparm.total = res.total
        }
      })
      .catch(err => {
        this.loading = false
        this.$message.error('响应列表加载失败，请稍后再试！')
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm = row
      } else {
        this.title = '添加'
        this.editForm = {
          responseType: '1',
          parsingMode: '1',
          content: '',
          at: '0',
          enable: '1',
          commandList: [{
            command: '',
            commandType: '1',
            at: '0'
          }]
        }
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          addResponse(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除响应配置
    deleteResponse(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteResponse(row.id)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 新增
    addRow(index, row) {
      this.editForm.commandList.push({
        command: '',
        commandType: '1',
        at: '0'
      })
    },
    // 删除
    delRow(index, row) {
      this.editForm.commandList.splice(index, 1)
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
      editForm = {}
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 